package cn.zc.curriculum.service.service.impl;


import cn.tools.common.dateTime.DateTimeUtil;
import cn.tools.common.enums.TestedFile;
import cn.tools.core.exception.BizException;
import cn.zc.curriculum.entity.UploadFilePaths;
import cn.zc.curriculum.service.config.UploadConfig;
import cn.zc.curriculum.service.service.UploadFileService;
import cn.zc.curriculum.service.service.UploadFileServiceAsync;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

@Service
public class UploadFileServiceImpl implements UploadFileService {
    @Resource
    private UploadConfig uploadConfig;

    @Resource
    private UploadFileServiceAsync uploadFileServiceAsync;

    /**
     * 检查配置，判断是否要存入网上对应点
     * @param fileStream
     * @param fileSavePath
     * @param fileType
     */
    public void uploadFileTestedCloud(InputStream fileStream, String fileSavePath,TestedFile fileType){
        switch (fileType){
            case IMG:
                if(uploadConfig.isImgOssSave()) {
                    uploadFileServiceAsync.uploadToOss(fileStream,fileSavePath);
                }
                break;

            case VIDEO:
                if(uploadConfig.isVideoOssSave()) {
                    uploadFileServiceAsync.uploadToOss(fileStream,fileSavePath);
                }else if(uploadConfig.isVideoBlwsSave()){
                    return;
                }
                break;
        }
    }







    /**
     * 检查文件是否为空或者没有后缀名
     * @param file
     */
    @Override
    public UploadFilePaths testedFile(MultipartFile file) {
        if(file == null){
            throw new BizException("上传文件为空，不可上传空文件");
        }
        //获取名称并检查名称是否为空
        String originalFilename = file.getOriginalFilename();
        if(originalFilename == null || originalFilename.equals("")){
            throw new BizException("文件名为空，上传失败");
        }
        String endName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1).toLowerCase();

        DateTimeUtil dateTimeUtil = new DateTimeUtil();
        String uuidName = dateTimeUtil.getRandomTime();

        String filePath= uploadConfig.getAliOssObjectName() + "/" + dateTimeUtil.getStringDate()  ;
        String fileSavePath = filePath + "/" + uuidName + "." + endName;
        String ossSavePath = uploadConfig.getAliOssUrl() + uploadConfig.getAliOssEndpoint() + "/" + fileSavePath;
        String localPath = uploadConfig.getLocalSavePath() + filePath + "/" + uuidName + "." + endName;

        UploadFilePaths uploadFilePaths = new UploadFilePaths();
        uploadFilePaths.setEndName(endName);
        uploadFilePaths.setFilePath(filePath);
        uploadFilePaths.setFileSavePath(fileSavePath);
        uploadFilePaths.setLocalPath(localPath);
        uploadFilePaths.setOssSavePath(ossSavePath);
        return uploadFilePaths;
    }





    /**
     * 判断文件是否要存放在本地
     * @param fileStream
     * @param filePath
     * @param localPath
     * @param fileType
     * @throws IOException
     */

    public void saveLocalTested(InputStream fileStream, String filePath, String localPath, TestedFile fileType) throws IOException{
        //判断是否要保存在本地
        switch (fileType){
            case IMG:
                if(uploadConfig.isImgLocalSave()) {
                    uploadFileServiceAsync.saveLocal(fileStream, filePath, localPath);
                }

                break;

            case VIDEO:
                if(uploadConfig.isVideoLocalSave()) {
                    uploadFileServiceAsync.saveLocal(fileStream, filePath, localPath);
                }
                break;
        }
    }


}
